javascript - rspec 单击一个跨度
全部标签 我想测试是否使用特定参数递归调用方法。我的方法:classRecursabledefrec(arg)rec(7)unlessarg==7endenddescribeRecursabledoit"shouldrecurse"dor=Recursable.new('test')r.should_receive(:rec).with(0).orderedr.should_receive(:rec).with(7).orderedr.rec(0)endend出乎意料的是,RSpec失败了:expected:recwith(7)once,butreceivedit0times知道我的方法有什么问
在我实际操作的简化示例中,假设我有2次对数据库的调用:Repo.add(something_stringy)Repo.remove(something_floaty)我想对数据库调用使用mock,因为真正的调用将在别处进行测试:let(:repo){repo=double("Repo")repo.should_receive(:add).with(instance_of(String))repo.should_receive(:remove).with(instance_of(Float))repo}before{FakeKlass.const_set:Repo,repo}这一切都很好
致敬!我正在尝试使用如下所示的Rspec测试Controller方法:defemail_customer@quote=Quote.find(params[:quote_id])hash={quote:@quote,body:params[:email_body],subject:params[:email_subject]}QuoteMailer.email_customer(hash).deliverredirect_toedit_quote_pathparams[:quote_id]end相应的规范如下所示:describe'POSTemail_customer'dolet!(:q
-:total_cost_with_tax=>#,-:total_cost_without_tax=>#,-:total_last_installment_amount=>#,-:total_monthly_installment_amount=>#,-:total_tax=>#,+:total_cost_with_tax=>#,+:total_cost_without_tax=>#,+:total_last_installment_amount=>#,+:total_monthly_installment_amount=>#,+:total_tax=>#,因此,这些在我的一些测试中随
我想知道如何在另一个类的一个类的实例中调用一个方法。这是我想出来的classClassAdefmethodreturn"Thisisamethod_from_class_A"endendclassClassBdefinitialize@method_from_class_A=instance.methodenddefmethod_calls_method@method_from_class_Aendendinstance=ClassA.newinstance2=ClassB.newputsinstance2.method_calls_method但是我得到这个错误:Testing.rb
我在Controller中创建了一些公共(public)方法来完成一些工作。defcurrent_serviceservice_name=params[:controller].gsub('api/v1/','').gsub(%r{/.+},'')end我想使用RSpec测试此方法,但我不知道如何stub参数。我应该怎么做? 最佳答案 如果这是一个Controller规范,你应该能够做这样的事情:allow(controller).toreceive(:params).and_return({controller:'avalue'}
我不太确定如何表达这一点,所以我只是举个例子。如果我写:some_method(["a","b"],3)我希望它返回某种形式的[{"a"=>0,"b"=>3},{"a"=>1,"b"=>2},{"a"=>2,"b"=>1},{"a"=>3,"b"=>0}]如果我传入some_method(%w(abc),2)期望的返回值应该是[{"a"=>2,"b"=>0,"c"=>0},{"a"=>1,"b"=>1,"c"=>0},{"a"=>1,"b"=>0,"c"=>1},{"a"=>0,"b"=>2,"c"=>0},{"a"=>0,"b"=>1,"c"=>1},{"a"=>0,"b"=>0,"
我已经使用了一段时间的shoulda,并且阅读并使用了rspec。我没有做过深入的比较和对比。但在我看来,两者之间有一些重叠,但它们不是1-1的替代品。我正在考虑使用rspec在我的rails系统中编写一些单元测试,而不替换所有使用shoulda编写的现有测试。只是作为一种获得感觉的方式。这是个好主意吗?我可以逐渐从一个转移到另一个还是自找麻烦?我应该考虑其中一个比另一个明显的优势吗?谢谢! 最佳答案 我不得不反对Chris的回答,即它们是替代方案。我在我的Rails应用程序中同时使用Shoulda和Rspec,它们相互补充得很好。
我认为这可能有效"abcdefghijk".each{|c|putcc;sleep0.25}我希望看到“abcdefj”一次打印一个字符,每个字符之间间隔0.25秒。但是整个字符串是一次打印出来的。 最佳答案 两件事:您需要使用.each_char来遍历字符。在Ruby1.8中,String.each将逐行进行。在Ruby1.9中,String.each已弃用。如果您希望字符立即出现,您应该手动刷新$stdout。否则,它们往往会被缓冲,以便字符在最后一次出现。.#!/usr/bin/envruby"abcddefghijk".ea
我想分解这堆代码,以便我所有的Controller测试(好吧,几乎所有的)都使用这个before(:each)block:before(:each)do@user=User.newcontroller.stub(:authenticate_user!)controller.stub(:current_user).and_return(@user)controller.stub(:add_secure_model_data)end有什么办法吗?我不想将它包含在所有Controller中......因为有一些不需要它。基本上,每个从SecureController扩展的Controller